from random import randint;

import pygame

from sprite.move_sprite import MoveSprite;


class Tank(MoveSprite):
    width = 50
    height = 50
    images = {
        'R': pygame.image.load('images/p1tankR.gif')
    }

    # 获取surface
    def get_surface(self):
        return self.images[self.direction]

    def __init__(self, left, top):
        super().__init__()
        # 坦克移动状态
        self.moveState = False
        # 坦克位置
        self.postion.left = left
        self.postion.top = top

    # 坦克移动
    def move(self, screen):
        if self.moveState and self.active:
            if self.direction == 'L':
                self.postion.left -= self.speed
                if self.postion.left < 0:
                    self.postion.left = 0
            elif self.direction == 'R':
                self.postion.left += self.speed
                if self.postion.right > screen.get_width():
                    self.postion.right = screen.get_width()
            elif self.direction == 'U':
                self.postion.top -= self.speed
                if self.postion.top < 0:
                    self.postion.top = 0
            elif self.direction == 'D':
                self.postion.top += self.speed
                if self.postion.bottom > screen.get_height():
                    self.postion.bottom = screen.get_height()

    def fire(self, missle):
        missle.setTank(self)


class PlayerTank(Tank):
    images = {
        'U': pygame.image.load('images/p1tankU.gif'),
        'D': pygame.image.load('images/p1tankD.gif'),
        'L': pygame.image.load('images/p1tankL.gif'),
        'R': pygame.image.load('images/p1tankR.gif')
    }

    def __init__(self, left, top):
        super().__init__(left, top)


class EnemyTank(Tank):
    images = {
        'U': pygame.image.load('images/enemy1U.gif'),
        'D': pygame.image.load('images/enemy1D.gif'),
        'L': pygame.image.load('images/enemy1L.gif'),
        'R': pygame.image.load('images/enemy1R.gif')
    }
    step = 10  # 敌人走多少变换方向
    speed = 3  # 敌人速度减慢

    def __init__(self):
        super().__init__(randint(1, 8) * 100, 30)
        self.moveState = True  # 敌人始终移动的
        self.get_init_state()

    def get_init_state(self):
        self.step = randint(0, 5) * 10  # 敌人走多少变换方向

        x = randint(0, 3)
        if x == 0:
            self.direction = 'U'
        elif x == 1:
            self.direction = 'D'
        elif x == 2:
            self.direction = 'L'
        else:
            self.direction = 'R'

    def move(self, screen):
        if self.active:
            if self.step == 0:
                self.get_init_state()
            else:
                super().move(screen)
                self.step -= 1
